package com.aixin.project.servlet;

import com.aixin.project.data.*;
import com.aixin.project.handler.ExpertHandler;
import com.aixin.project.handler.UserHandler;
import com.aixin.project.util.AxMemcached;
import com.aixin.project.util.AxUtil;
import com.aixin.project.util.RequestUtil;
import org.apache.velocity.Template;
import org.apache.velocity.context.Context;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet extends BaseServlet {

    @Override
    protected Template execute(HttpServletRequest request,
                               HttpServletResponse response, Context ctx) {
        AxSession axsession = (AxSession) request.getSession().getAttribute(
                CommonConstants.USER_SESSION);
        String method = RequestUtil.getStr(request, "method", null);
        if (method == null) return getTemplate("login.vm");
        String username = request.getParameter("username");
        String password = RequestUtil.toMD5(request.getParameter("password"));
        String vcode = request.getParameter("vcode");
        if ("u".equals(method)) {
            User user = null;
            if (null == axsession) {
                user = UserHandler.find(username, password);
                if (null != user) {
                    axsession = new AxSession(UserType.USER, user.getId(), (null == vcode) ? false : true, RequestUtil.getGenseeUid(request,response));
                    request.getSession().setAttribute(CommonConstants.USER_SESSION, axsession);
                }
            } else {
                user = UserHandler.find(axsession.getUid());
                if (null == user)
                    user = UserHandler.find(username, password);
            }

            if (null != user) {
                if (null != vcode)
                    RequestUtil.setCookie("u", "u_" + user.getId(), CommonConstants.COOKIE_AGE, "/", response);
                AxMemcached.set(CommonConstants.MKEY_USER + user.getId(), user);
                axsession = new AxSession(UserType.USER, user.getId(), (null == vcode) ? false : true, RequestUtil.getGenseeUid(request,response));
                request.getSession().setAttribute(CommonConstants.USER_SESSION, axsession);
                this.sendRedirect(response, "/");
            } else {
                request.getSession().setAttribute("ERROR", "用户名密码错误，或者该账号审核中，请与客服人员联系，客服电话 " + CommonConstants.SERVICETEL);
                this.sendRedirect(response, "/login");
            }
        } else if ("e".equals(method)) {
            Expert expert = null;
            if (null == axsession) {
                expert = ExpertHandler.find(username, password);
                if (null != expert) {
                    axsession = new AxSession(UserType.EXPERT, expert.getId(), (null == vcode) ? false : true, RequestUtil.getGenseeUid(request,response));
                    request.getSession().setAttribute(CommonConstants.USER_SESSION, axsession);
                }
            } else {
                expert = ExpertHandler.find(axsession.getUid());
                if (null == expert)
                    expert = ExpertHandler.find(username, password);
            }

            if (null != expert) {
                if (null != vcode)
                    RequestUtil.setCookie("u", "e_" + expert.getId(), CommonConstants.COOKIE_AGE, "/", response);
                AxMemcached.set(CommonConstants.MKEY_EXPERT + expert.getId(), expert);
                axsession = new AxSession(UserType.EXPERT, expert.getId(), (null == vcode) ? false : true,RequestUtil.getGenseeUid(request,response));
                request.getSession().setAttribute(CommonConstants.USER_SESSION, axsession);
                this.sendRedirect(response, "/");
            } else
                this.sendRedirect(response, "/login");
        } else {
            return getTemplate("login.vm");
        }

        return null;
    }

}
